from Socket_msg_view import socketio
from app import *
from flask import request,jsonify
from tools import MyMongo,MyRedis
import user_view
import Customer_service
app.register_blueprint(user_view.user)
app.register_blueprint(Customer_service.customer_ser)


@app.route('/',methods=['GET','POST',"PUT","DELETE"])
def hello_world():
    #res = db.session.execute("insert into user (`username`) values ('123') ")
    res = db.session.execute(" select id,username from user ").fetchall()
    data = request.args.get("id")
    #data = request.form.get("id")
    print(data)
    print(res)
    #return 'Hello Flask'
    return jsonify({'result': [dict(row) for row in res]})

def job_1():  # 一个函数，用来做定时任务的任务。 定期同步到mongo
    import arrow
    n = arrow.now()
    with MyRedis() as r:
        # 拿出统计数据
        check_list = r.hgetall(n.format('YYYY-MM-DD'))
        check_dict={}
        check_num=0
        for k, v in check_list.items(): # 封装进dict
            print(k,v)
            check_dict[k.decode('utf-8')]= v.decode('utf-8')
            check_num += int(v.decode('utf-8'))
        r.delete(n.format('YYYY-MM-DD'))
    with MyMongo(db='md', table='check_count') as c:
        c.insert_one({'time':n.format('YYYY-MM-DD'),'check_dict':check_dict,'check_num':check_num})


if __name__ == '__main__':
    app.run(debug=True,host="0.0.0.0",port=5000)
    # socketio.run(app, debug=True, host="0.0.0.0", port=5000)